<template>
  <div>

    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Overview' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理员表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--    -->
    <el-card class="title">
      <h1>管理员表</h1>
    </el-card>

    <!--卡片视图区域-->
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisble = true">添加管理员</el-button>
        </el-col>
      </el-row>

      <!--用户列表区域-->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="name" prop="name"></el-table-column>
        <el-table-column label="email" prop="email"></el-table-column>
        <el-table-column label="phone" prop="phone"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.name)"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini"
                       @click="removeUserById(scope.row.name)"></el-button>

          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!--添加用户的对话框-->
      <el-dialog
        @click="addDialogClosed"
        title="提示"
        :visible.sync="addDialogVisble"
        width="50%">
        <!-- 内容主体区域-->
        <el-form :model="addForm"  ref="addFormRef" label-width="100px">
          <el-form-item label="name" >
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item label="password" >
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <el-form-item label="email" >
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
          <el-form-item label="phone" >
            <el-input v-model="addForm.phone"></el-input>
          </el-form-item>
        </el-form>
        <!--底部区域-->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisble = false">取 消</el-button>
          <el-button type="primary" @click="addAccessType">确 定</el-button>
        </span>
      </el-dialog>

      <!--修改用户的对话框-->
      <el-dialog
        @close="editDialogClosed"
        title="提示"
        :visible.sync="editDialogVisible"
        width="50%">
        <el-form :model="editForm" ref="editFormRef" label-width="100px">
          <el-form-item label="name">
            <el-input v-model="editForm.name" disabled></el-input>
          </el-form-item>
          <el-form-item label="password" >
            <el-input v-model="editForm.psd"></el-input>
          </el-form-item>
          <el-form-item label="email" >
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>
          <el-form-item label="phone" >
            <el-input v-model="editForm.phone"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>

</template>

<script>
  import FlowChartDataHandle from '../../DataHandle/FlowChartDataHandle'

  export default {
    name: 'AccessTypeTable',
    data () {
      return {
        // 获取用户列表的参数对象
        queryInfo: {
          query:'',
          // 当前的页数
          pagenum: 1,
          // 当前每页显示多少条数据
          pagesize: 5
        },
        userlist: null,
        total: 3,
        // 控制添加用户对话框的显示与隐藏
        addDialogVisble: false,
        // 添加用户的表单数据
        addForm: {
          name: '',
          email: '',
          phone: '',
          password:''
        },
        // 控制编辑用户对话框的显示与隐藏
        editDialogVisible: false,
        // 查询到的用户信息对象
        editForm: {},
      }
    },
    created () {
      this.getUserList()
    },
    methods: {
      async getUserList () {
        let params = new URLSearchParams()
        params.append('query', this.queryInfo.query)
        params.append('pagenum', this.queryInfo.pagenum - 1)
        params.append('pagesize', this.queryInfo.pagesize)
        let userFindRes = await this.$http.post(
          '/zz/userFind',
          params
        )
        if (userFindRes.data.status !== 200) {
          return this.$message.error('添加失败！')
        }
        this.userlist = userFindRes.data.users
        console.log(userFindRes.data)
        this.total = parseInt(userFindRes.data.totalNum)
      },
      // 监听 pagesize 改变事件
      handleSizeChange (newSize) {
        console.log(newSize)
        this.queryInfo.pagesize = newSize
        this.getUserList()
      },
      // 监听页码值改变的事件
      handleCurrentChange (newPage) {
        console.log(newPage)
        this.queryInfo.pagenum = newPage
        this.getUserList()
      },
      // 监听添加用户对话框的关闭事件
      addDialogClosed () {
        this.$refs.addFormRef.resetFields()
      },
      // 点击按钮 添加用户
      async addAccessType () {
        let params = new URLSearchParams()
        params.append('name', this.addForm.name)
        params.append('psd', this.addForm.password)
        params.append('email', this.addForm.email)
        params.append('phone', this.addForm.phone)
        console.log(params)
        console.log(this.addForm)
        let insertOneUserInfoRes = await this.$http.post(
          '/zz/insertOneUserInfo',
          params
        )
        console.log(insertOneUserInfoRes)
        if (insertOneUserInfoRes.data.status !== 200) {
          return this.$message.error('添加失败！')
        }
        this.$message.success('添加成功')
        // 隐藏添加用户的对话框
        this.addDialogVisble = false
        // 重新获取用户列表
        this.getUserList()
      },
      // 展示用户编辑的对话框
      async showEditDialog (name) {
        let params = new URLSearchParams()
        console.log(name)
        params.append('name',name)
        let findOneUserInfoRes = await this.$http.post(
          '/zz/findOneUserInfo',
          params
        )
        console.log(findOneUserInfoRes)
        if (findOneUserInfoRes.data.status !== 200) {
          return this.$message.error('查询用户信息失败！')
        }
        console.log(findOneUserInfoRes.data)
        this.editForm = findOneUserInfoRes.data.users[0]
        console.log(this.editForm)
        this.editDialogVisible = true
      },
      // 监听修改用户对话框的关闭事件
      editDialogClosed () {
        this.$refs.editFormRef.resetFields()
      },
      // 修改用户信息并提交
      async editUserInfo () {
        console.log(this.editForm)
        let params = new URLSearchParams()
        params.append('name', this.editForm.name)
        params.append('psd', this.editForm.psd)
        params.append('phone', this.editForm.phone)
        params.append('email', this.editForm.email)
        console.log(params)

        let updateUserInfoRes = await this.$http.post(
          '/zz/updateUserInfo',
          params
        )
        console.log(updateUserInfoRes)
        if (updateUserInfoRes.data.status !== 200) {
          return this.$message.error('更新失败！')
        }
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getUserList()
        // 提示修改成功
        this.$message.success('更新信息成功')
      },
      // 根据Id删除对应的用户信息
      async removeUserById (name) {
        if (name == 'admin') {
          return this.$message.error("该用户不能删除")
        }
        // console.log(id)
        // 询问用户是否删除
        const confirmResult = await this.$confirm('此操作将永久删除该条信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async (success) => {
          let params = new URLSearchParams()
          params.append('name',name)
          let delUserInforRes = await this.$http.post(
            '/zz/delUserInfo',
            params
          )
          console.log(delUserInforRes)
          if (delUserInforRes.data.status !== 200) {
            return this.$message.error('查询用户信息失败！')
          }

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUserList()

          return success
        }).catch((err) => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          return err
        })
        console.log(confirmResult)
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
